<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-row :gutter="10">
        <el-col :span="8">
          <el-form-item label="收件人" prop="recipientName">
            <el-input
              v-model="queryParams.recipientName"
              placeholder="请输入收件人姓名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="邮寄状态" prop="shippingStatus">
            <el-select v-model="queryParams.shippingStatus" placeholder="请选择邮寄状态" clearable style="width: 100%">
              <el-option label="待处理" value="待处理" />
              <el-option label="已发货" value="已发货" />
              <el-option label="已签收" value="已签收" />
              <el-option label="退回" value="退回" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="追踪号" prop="trackingNumber">
            <el-input
              v-model="queryParams.trackingNumber"
              placeholder="请输入追踪号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="8">
          <el-form-item label="地区" prop="city">
            <el-input
              v-model="queryParams.city"
              placeholder="请输入城市"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="国家" prop="country">
            <el-input
              v-model="queryParams.country"
              placeholder="请输入国家"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="发货时间">
            <el-date-picker
              v-model="dateRange"
              style="width: 100%"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24" style="text-align: right">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:details:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:details:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:details:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:details:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="detailsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="邮寄ID" align="center" prop="shippingId" />
      <el-table-column label="订单ID" align="center" prop="orderId" />
      <el-table-column label="信件ID" align="center" prop="letterId" />
      <el-table-column label="收件人姓名" align="center" prop="recipientName" />
      <el-table-column label="地址行1" align="center" prop="addressLine1" />
      <el-table-column label="地址行2" align="center" prop="addressLine2" />
      <el-table-column label="城市" align="center" prop="city" />
      <el-table-column label="省/州/区域" align="center" prop="stateProvinceRegion" />
      <el-table-column label="邮政编码" align="center" prop="postalCode" />
      <el-table-column label="国家" align="center" prop="country" />
      <el-table-column label="电话号码" align="center" prop="phoneNumber" />
      <el-table-column label="联系人" align="center" prop="contactPerson" />
      <el-table-column label="邮寄状态" align="center" prop="shippingStatus" />
      <el-table-column label="追踪号" align="center" prop="trackingNumber" />
      <el-table-column label="发货时间" align="center" prop="shippedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.shippedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:details:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:details:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改纸质信件邮寄信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单ID" />
        </el-form-item>
        <el-form-item label="信件ID" prop="letterId">
          <el-input v-model="form.letterId" placeholder="请输入信件ID" />
        </el-form-item>
        <el-form-item label="收件人姓名" prop="recipientName">
          <el-input v-model="form.recipientName" placeholder="请输入收件人姓名" />
        </el-form-item>
        <el-form-item label="地址行1" prop="addressLine1">
          <el-input v-model="form.addressLine1" placeholder="请输入地址行1" />
        </el-form-item>
        <el-form-item label="地址行2" prop="addressLine2">
          <el-input v-model="form.addressLine2" placeholder="请输入地址行2" />
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入城市" />
        </el-form-item>
        <el-form-item label="省/州/区域" prop="stateProvinceRegion">
          <el-input v-model="form.stateProvinceRegion" placeholder="请输入省/州/区域" />
        </el-form-item>
        <el-form-item label="邮政编码" prop="postalCode">
          <el-input v-model="form.postalCode" placeholder="请输入邮政编码" />
        </el-form-item>
        <el-form-item label="国家" prop="country">
          <el-input v-model="form.country" placeholder="请输入国家" />
        </el-form-item>
        <el-form-item label="电话号码" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入电话号码" />
        </el-form-item>
        <el-form-item label="联系人" prop="contactPerson">
          <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="追踪号" prop="trackingNumber">
          <el-input v-model="form.trackingNumber" placeholder="请输入追踪号" />
        </el-form-item>
        <el-form-item label="发货时间" prop="shippedAt">
          <el-date-picker clearable
            v-model="form.shippedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDetails, getDetails, delDetails, addDetails, updateDetails } from "@/api/system/details";

export default {
  name: "Details",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 纸质信件邮寄信息表格数据
      detailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        letterId: null,
        recipientName: null,
        addressLine1: null,
        addressLine2: null,
        city: null,
        stateProvinceRegion: null,
        postalCode: null,
        country: null,
        phoneNumber: null,
        contactPerson: null,
        shippingStatus: null,
        trackingNumber: null,
        shippedAt: null,
        createdAt: null,
        updatedAt: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderId: [
          { required: true, message: "订单ID不能为空", trigger: "blur" }
        ],
        letterId: [
          { required: true, message: "信件ID不能为空", trigger: "blur" }
        ],
        recipientName: [
          { required: true, message: "收件人姓名不能为空", trigger: "blur" }
        ],
        addressLine1: [
          { required: true, message: "地址行1不能为空", trigger: "blur" }
        ],
        city: [
          { required: true, message: "城市不能为空", trigger: "blur" }
        ],
        postalCode: [
          { required: true, message: "邮政编码不能为空", trigger: "blur" }
        ],
        country: [
          { required: true, message: "国家不能为空", trigger: "blur" }
        ],
        createdAt: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updatedAt: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询纸质信件邮寄信息列表 */
    getList() {
      this.loading = true;
      listDetails(this.queryParams).then(response => {
        this.detailsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        shippingId: null,
        orderId: null,
        letterId: null,
        recipientName: null,
        addressLine1: null,
        addressLine2: null,
        city: null,
        stateProvinceRegion: null,
        postalCode: null,
        country: null,
        phoneNumber: null,
        contactPerson: null,
        shippingStatus: null,
        trackingNumber: null,
        shippedAt: null,
        createdAt: null,
        updatedAt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.dateRange && this.dateRange.length > 0) {
        this.queryParams.beginShippedTime = this.dateRange[0];
        this.queryParams.endShippedTime = this.dateRange[1];
      } else {
        this.queryParams.beginShippedTime = undefined;
        this.queryParams.endShippedTime = undefined;
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.shippingId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加纸质信件邮寄信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const shippingId = row.shippingId || this.ids
      getDetails(shippingId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改纸质信件邮寄信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.shippingId != null) {
            updateDetails(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDetails(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const shippingIds = row.shippingId || this.ids;
      this.$modal.confirm('是否确认删除纸质信件邮寄信息编号为"' + shippingIds + '"的数据项？').then(function() {
        return delDetails(shippingIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/details/export', {
        ...this.queryParams
      }, `details_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
